package com.yungu.swift.socket.component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.assets.driver.service.DriverService;
import com.yungu.swift.assets.passenger.service.PassengerService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.socket.communication.message.LoginMessage;
import org.springframework.stereotype.Component;

/**
 * @author : cuixiuyin
 * @date : 2019/5/13
 */
@Component
public class LoginUserUtils {

    private static DriverService driverService;

    private static PassengerService passengerService;


    @Reference
    public void setDriverService(DriverService driverService) {
        LoginUserUtils.driverService = driverService;
    }

    @Reference
    public void setPassengerService(PassengerService passengerService) {
        LoginUserUtils.passengerService = passengerService;
    }

    public static Boolean loginUser(LoginMessage loginMessage) {
        ResponseData<Boolean> responseData = ResponseData.buildSuccessResponse(Boolean.FALSE);
        if (LoginMessage.From.PASSENGER.equals(loginMessage.getFrom())) {
            responseData = passengerService.isPassenger(loginMessage.getClientUuid(), loginMessage.getToken());
        }
        if (LoginMessage.From.DRIVER.equals(loginMessage.getFrom())) {
            responseData = driverService.isDriver(loginMessage.getClientUuid(), loginMessage.getToken());
        }
        if (responseData.isSuccess() && Boolean.TRUE.equals(responseData.getData())) {
            return true;
        }
        return false;
    }
}
